from .celery import app
from django.core.cache import cache
from django.conf import settings
from home import serializers, models


@app.task
def update():

    banner_queryset = models.Banner.objects.filter(is_delete=False, is_show=True).order_by('-orders')

    if not settings.BANNER_COUNT > len(banner_queryset):
        banner_queryset = banner_queryset[:settings.BANNER_COUNT]

    banner_list = serializers.BannerModelSerializer(instance=banner_queryset, many=True).data
    for banner in banner_list:
        banner['image'] = 'http://127.0.0.1:8000%s' % banner['image']
    cache.set(settings.BANNER_CACHE_KEY, banner_list, 60*60*24)
    print(banner_list)
    return True

